package com.school.service.impl;


import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.school.dao.HljtTeacherDao;
import com.school.dao.HljtUserDao;
import com.school.entity.HljtJurisdiction;
import com.school.entity.HljtTeacher;
import com.school.entity.HljtUser;
import com.school.entity.web.HljtUserInfo;
import com.school.entity.web.UseresloginParameter;
import com.school.service.HljtUserService;
import com.school.util.MapperToJson;
import com.school.util.Md5;
import com.school.util.RepMessage;
import com.school.util.WhetherNotToUpdate;


@Service
public class HljtUserServiceImpl implements HljtUserService{

	@Autowired
	HljtUserDao oDao;
	private boolean flase;
	@Autowired
	private HljtTeacherDao hljtTeacherDao;
	
	@Override
	public String userlogin(HljtUser oHljtUser) {
		RepMessage oRepMassage=new RepMessage();
	String userName=oHljtUser.getUsername();
	String passWord=Md5.md5Password(oHljtUser.getPassword());
	HljtUserInfo oHljtUserInfo=new HljtUserInfo();
	oHljtUserInfo =	oDao.userlogin(userName,passWord);
	if(StringUtils.isEmpty(oHljtUserInfo)){
		oRepMassage.setFlag(false);
	}else{
		oRepMassage.setFlag(true);
		oRepMassage.setObjData(oHljtUserInfo.getPageId());
	}
	return MapperToJson.toJson(oRepMassage);
	}
	@Override
	public int userUpdate(HljtUser oHljtUser) {
		int nCount=oDao.selectByUserName(oHljtUser.getUsername());
		if(nCount!=0){
			oHljtUser.setPassword(Md5.md5Password(oHljtUser.getPassword()));
			nCount=oDao.userUpdate(oHljtUser);
			WhetherNotToUpdate.setPattern(true);
		}
		return nCount;
	}
	
	@Override
	public int userInsert(HljtUser oHljtUser) {
		int nCount=oDao.selectByUserName(oHljtUser.getUsername());
		if(nCount==0){
			oHljtUser.setPassword(Md5.md5Password(oHljtUser.getPassword()));
			nCount=oDao.userInsert(oHljtUser);
			WhetherNotToUpdate.setPattern(true);
		}else{
			nCount=0;
		}
		return nCount;
	}
	@Override
	public int userDelete(String userName) {
		int nCount=oDao.selectByUserName(userName);
		if(nCount!=0){
			nCount=oDao.userDelete(userName);
			WhetherNotToUpdate.setPattern(true);
		}
		return nCount;
	}
	
	
	
	@SuppressWarnings("unused")
	private List<HljtJurisdiction> getPermissionpage(Integer pageId) {
		HljtUser hljtUser = new HljtUser();
		List<Integer> list = new ArrayList<>();
		if(pageId==1) {
			list = hljtUser.getOrdinaryJurisdiction();
		}else if(pageId==2) {
			list = hljtUser.getClassJurisdiction();
		}else if(pageId==3) {
			list = hljtUser.getHighestJurisdiction();
		}
		List<HljtJurisdiction> hljtJurisdiction = oDao.allJurisdictionByParent(list);
		List<HljtJurisdiction> hljtJu = new ArrayList<>();
		Map<Integer, HljtJurisdiction> map = new HashMap<>();
			if(null !=hljtJurisdiction) {
				for(HljtJurisdiction hljtSchoolnotify: hljtJurisdiction) {
			    Integer key = hljtSchoolnotify.getParent();
			    HljtJurisdiction value = hljtSchoolnotify;
			    if(map.containsKey(key)) {
			    	HljtJurisdiction ls = map.get(key);
			    	List<HljtJurisdiction> hljtJur=ls.getChildren();
			    	hljtJur.add(value);
			    }else {
			    	map.put(key, value);
			    	List<HljtJurisdiction> hljtJuri = new ArrayList<>();
			    	value.setChildren(hljtJuri);
			        hljtJu.add(value);
			    }
			}
		}
		return hljtJu;
	}
	@Transactional
	@Override
	public UseresloginParameter usereslogin(HljtUser hljtUser) {
		HljtUser re =  oDao.usereslogin(hljtUser);
		UseresloginParameter useresloginParameter=new UseresloginParameter();
		if(null !=re && re.getPassword().equals(hljtUser.getPassword()) && re.getUsername().equals(hljtUser.getUsername())){
			//登陆成功
			
			hljtUser.setTeacherid(re.getTeacherid());
			 useresloginParameter = oDao.useresloginJurisdiction(hljtUser);
			 useresloginParameter.setWhetherLandingSuccess(true);
			if(useresloginParameter.getPageId().equals("2")) {
				Integer classId = oDao.useresloginClassIsNotBound(hljtUser);
				if(null==classId) {
					//是班主任但没有绑定班级
					useresloginParameter.setClassId(-3);
				}else {
					//绑定的班级
					useresloginParameter.setClassId(classId);
					useresloginParameter.setUrl("personalinformation.html");
				}
			}else if(useresloginParameter.getPageId().equals("3")){
				//最高权限
				useresloginParameter.setClassId(-1);
				useresloginParameter.setUrl("homepage.html");
			}else if(useresloginParameter.getPageId().equals("1")){
				//普通老师
				useresloginParameter.setClassId(-2);
				useresloginParameter.setUrl("personalinformation.html");
			}
			useresloginParameter.setTeacherId(re.getTeacherid());
		}
		return useresloginParameter;
	}
	@Override
	@Transactional
	public Map<String, Object> getPermissionpage(Integer teacherId, Integer pageId) {
		
		Map<String, Object> map = new HashMap<>();
		HljtTeacher d = hljtTeacherDao.findTeacher(teacherId);
		try {
			d.setImages(WhetherNotToUpdate.getIp()+d.getImages());
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		map.put("user", d);
		map.put("Userinfo", this.getPermissionpage(pageId));
		SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		map.put("date", time.format(new Date()));
		return map;
	}
}
